from rest_framework.permissions import DjangoModelPermissions, IsAdminUser

from apps.orders.models import OrderInfo,OrderGoods,SKU
from rest_framework.generics import ListAPIView,RetrieveAPIView,UpdateAPIView
from apps.wthg_admin.serializers.orders import OrderInfoModelSerializer,OrderInfoGoodsModelSerializer,OrderInfoUpdateModelSerializer
from apps.wthg_admin.utils import UtilsPageNumberPagination
from rest_framework.response import Response

class OrderInfoListUpdateAPIView(ListAPIView,UpdateAPIView):
    """订单管理查看和修改"""

    serializer_class = OrderInfoModelSerializer
    pagination_class = UtilsPageNumberPagination


    def get_queryset(self):
        if self.request.query_params.get('keyword') is None or self.request.query_params.get('keyword') == '':
            return OrderInfo.objects.all()

        return OrderInfo.objects.filter(order_id__contains=self.request.query_params.get('keyword'))

    def list(self, request, *args, **kwargs):
        """订单列表展示"""
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        for item in serializer['lists']:
            item['create_time']=item['create_time'].strftime("%Y-%m-%d %H:%M:%S")
        return Response(serializer.data)



class OrderGoodsRetrieveAPIView(RetrieveAPIView):
    """订获取订单详情数据"""
    def get_queryset(self):
        return  OrderInfo.objects.filter(order_id=self.kwargs.get('pk'))

    serializer_class = OrderInfoGoodsModelSerializer


class OrderInfoUpdateAPIView(UpdateAPIView):
    """更新订单状态视图"""
    lookup_field ='order_id'

    def get_queryset(self):
        return OrderInfo.objects.filter(order_id=self.kwargs.get('order_id'))

    serializer_class = OrderInfoUpdateModelSerializer

